import { defineConfig } from '@rsbuild/core';
import { pluginSass } from '@rsbuild/plugin-sass';
import { pluginVue } from '@rsbuild/plugin-vue';
import { pluginVueJsx } from '@rsbuild/plugin-vue-jsx';
import { presetAttributify } from '@unocss/preset-attributify';
import { presetUno } from '@unocss/preset-uno';
import { UnoCSSRspackPlugin } from '@unocss/webpack/rspack';

import AutoImport from 'unplugin-auto-import/rspack';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import Components from 'unplugin-vue-components/rspack';

import { pluginImageCompress } from '@rsbuild/plugin-image-compress';

const target = `${process.env.PUBLIC_PROXY}`;
const port = Number.parseInt(`${process.env.PUBLIC_PORT}` as string, 10);
const base = `${process.env.PUBLIC_BASE_URL}`;

export default defineConfig({
  plugins: [
    pluginVue(),
    pluginVueJsx(),
    pluginSass({
      sassLoaderOptions: {
        sourceMap: true,
        additionalData: `@use "@/styles/element/index.scss" as *;`,
      },
    }),
    pluginImageCompress(),
  ],
  tools: {
    rspack: {
      plugins: [
        UnoCSSRspackPlugin({
          presets: [presetUno(), presetAttributify()],
        }),
        AutoImport({
          include: [/\.[tj]sx?$/, /\.vue$/, /\.vue\?vue/, /\.md$/],
          imports: [
            'vue',
            'vue-router',
            {
              '@vueuse/core': ['useMouse', ['useFetch', 'useMyFetch']],
              axios: [['default', 'axios']],
            },
            {
              from: 'vue-router',
              imports: ['RouteLocationRaw'],
              type: true,
            },
          ],
          ignore: ['useMouse', 'useFetch'],
          defaultExportByFilename: false,
          dirs: [
            'src/composable/*',
            'src/composable/**/index.ts',
            'src/constants/*',
            'src/constants/**/index.ts',
            'src/config/*',
            'src/config/**/index.ts',
            'src/enum/*',
            'src/enum/**/index.ts',
            'src/router/*',
            'src/router/**/index.ts',
            'src/store/*',
            'src/store/**/index.ts',
            'src/utils/*',
            'src/utils/**/index.ts',
            'src/api/*',
            'src/api/**/index.ts',
          ],
          dts: 'src/dts/auto-imports.d.ts',
          ignoreDts: ['ignoredFunction', /^ignore_/],
          vueTemplate: false,
          resolvers: [
            ElementPlusResolver({
              importStyle: 'sass',
            }),
          ],
          viteOptimizeDeps: true,
          injectAtEnd: true,
          biomelintrc: {
            enabled: false,
            filepath: './.biomelintrc-auto-import.json',
          },
        }),
        Components({
          include: [/\.vue$/, /\.vue\?vue/],
          resolvers: [
            ElementPlusResolver({
              importStyle: 'sass',
            }),
          ],
          dts: 'src/dts/components.d.ts',
          deep: true,
          dirs: ['src/components', 'src/views'],
        }),
      ],
    },
  },

  server: {
    base,
    port,
    proxy: {
      '^/ui/(cpoe|sde|ca|rfs|pps|mips|test|mpi|uum|apm|mdm|rvp|das|pts|oai|bms|mqa|pds|urp|cim|isms|rcs|pay|qms|disd)':
        {
          target,
          changeOrigin: false,
        },
      '/apm-bff-server': {
        target,
        changeOrigin: false,
      },
      '/serverTime': {
        target,
        changeOrigin: false,
      },
      '/browser/perfData': {
        target: 'http://192.168.20.43:12800',
      },
      '/browser/errorLogs': {
        target: 'http://192.168.20.43:12800',
      },
      '/v3/segments': {
        target: 'http://192.168.20.43:12800',
      },
      '/login': {
        target,
        changeOrigin: false,
      },
      '/logout': {
        target,
        changeOrigin: false,
      },
      '/oauth2': {
        target,
        changeOrigin: false,
      },
      '/cas': {
        target,
        changeOrigin: false,
      },
    },
  },
  source: {
    entry: {
      index: './src/main.ts',
    },
    alias: {
      '@': './src',
    },
  },
  html: {
    template: './public/index.html',
  },
  performance: {
    chunkSplit: {
      strategy: 'split-by-module',
      forceSplitting: {
        axios: /node_modules[\\/]axios/,
      },
    },
  },
});
